﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Map : MonoBehaviour
{
    // 围墙预制体
    public GameObject[] OutWalls;
    // 食物预制体
    public GameObject[] Foods;
    // 障碍物预制体
    public GameObject[] Obstacles;
    // 地板预制体
    public GameObject[] Floors;
    // 怪物预制体
    public GameObject[] Enemys;
    // 玩家
    // public GameObject Player;
    // 出口
    public GameObject Exit;
    // 当前关卡
    private int _levelGame;
    // 空白点
    private List<MyPoint> _pointAll = new List<MyPoint>();
    // 每8关 地图大小增加1
    // 每2关 障碍物 增加1
    // 每3关 食物增加 2
    // 每4关 怪物增加 1
    // 初始化地图
    public void init(int levelGame) {
        _levelGame = levelGame;
        StaticData.ScreenH += (int)(levelGame / StaticData.mapAdd);
        StaticData.ScreenW += (int)(levelGame / StaticData.mapAdd); 

        Camera.main.orthographicSize = StaticData.ScreenH * 0.5f;
        float xCamera = (StaticData.ScreenW - 1) * 0.5f;
        float yCamera = (StaticData.ScreenH - 1) * 0.5f;
        Camera.main.transform.position = new Vector3(xCamera, yCamera, -10);

        for (int x = 0; x < StaticData.ScreenW; x++)
        {
            for (int y = 0; y < StaticData.ScreenH; y++)
            {
                // 四周放围墙
                if (x == 0 || x == StaticData.ScreenW - 1 || y == 0 || y == StaticData.ScreenH - 1) {
                    GameObject outWall = Instantiate(OutWalls[Random.Range(0, OutWalls.Length)], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    outWall.transform.SetParent(transform);
                } else {
                    _pointAll.Add(new MyPoint(x, y, EnumType.TYPE_black));
                    GameObject floor = Instantiate(Floors[Random.Range(0, Floors.Length)], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    floor.transform.SetParent(transform);
                }
            }   
        }

        initObstacles();
        initPlayer();
        initExit();
        initFoods();
        initEnemy();
    }
    /// <summary>
    /// 初始化障碍物
    /// </summary>
    public void initObstacles() {
        // 障碍物的个数
        StaticData.numObstacle += (int)(_levelGame / StaticData.obstacleAdd);
        int num = StaticData.numObstacle;
        while (num != 0)
        {
            int i = Random.Range(0, _pointAll.Count);
            MyPoint point = _pointAll[i];
            if (point.type == EnumType.TYPE_black 
                && point.x != 1 
                && point.y != 1 
                && point.x != StaticData.ScreenW - 2 
                && point.y != StaticData.ScreenH - 2) {
                point.type = EnumType.TYPE_obstacle;
                // _pointAll.Insert(i, point);
                _pointAll[i] = point;
                num--;
                GameObject obstacle = Instantiate(Obstacles[Random.Range(0, OutWalls.Length)], new Vector3(point.x, point.y, 0), Quaternion.identity) as GameObject;
                obstacle.transform.SetParent(transform);
            }
        }
    }
     /// <summary>
    /// 初始化出口
    /// </summary>
    public void initExit() {
        GameObject exit = Instantiate(Exit, new Vector3(StaticData.ScreenW - 2, StaticData.ScreenH - 2, 0), Quaternion.identity) as GameObject;
        for (int i = 0; i < _pointAll.Count; i++)
        {
            MyPoint point = _pointAll[i];
            if (point.x == StaticData.ScreenW - 2 && point.y == StaticData.ScreenH - 2) {
                point.type = EnumType.TYPE_exit;
                // _pointAll.Insert(i, point);
                _pointAll[i] = point;
            }
        }
    }
    /// <summary>
    /// 初始化食物
    /// </summary>
    public void initFoods() {
        // 初始化食物的个数
        StaticData.numFood += (int)(_levelGame / StaticData.foodAdd);
        int num = StaticData.numFood;
        while (num != 0)
        {
            int i = Random.Range(0, _pointAll.Count);
            MyPoint point = _pointAll[i];
            if (point.type == EnumType.TYPE_black) {
                point.type = EnumType.TYPE_food;
                _pointAll[i] = point;
                num--;
                GameObject food = Instantiate(Foods[Random.Range(0, Foods.Length)], new Vector3(point.x, point.y, 0), Quaternion.identity) as GameObject;
                food.transform.SetParent(transform);
            }
        }
    }

    // 初始化玩家
    public void initPlayer() {
        // 初始化玩家
        // GameObject player = Instantiate(Player, new Vector3(1, 1, 0), Quaternion.identity) as GameObject;
        int a = _pointAll.Count;
        for (int i = 0; i < a; i++)
        {
            MyPoint point = _pointAll[i];
            if (point.x == 1 && point.y == 1) {
                point.type = EnumType.TYPE_player;
                _pointAll[i] = point;
            }
        }
    }
   
    // 初始化怪物
    public void initEnemy() {     
        // 初始化怪物
        StaticData.numEnemy += (int)(_levelGame / StaticData.enemyAdd);
        int num = StaticData.numEnemy;
        while (num != 0)
        {
            int i = Random.Range(0, _pointAll.Count);
            MyPoint point = _pointAll[i];
            if (point.type == EnumType.TYPE_black) {
                point.type = EnumType.TYPE_enemy;
                _pointAll[i] = point;
                num--;
                GameObject enemy = Instantiate(Enemys[Random.Range(0, Enemys.Length)], new Vector3(point.x, point.y, 0), Quaternion.identity) as GameObject;
                // food.transform.SetParent(transform);
            }
        }
    }
}
